import { getAcademic, getCampusSelector } from '@/api/campus/campus'
import request from '@/utils/request'
import FlowBox from '@/views/workFlow/components/FlowBox'
import CustomForm from './Form/index.vue'
export default {
  components: { CustomForm, FlowBox },
  data() {
    return {
      showAll: false,
      query: {
        brand: 'bell',
        campusId: undefined,
        counselor: undefined,
        startDate: undefined,
        type: undefined
      },
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'createTime'
      },
      formVisible: false,
      flowVisible: false,
      exportBoxVisible: false,
      columnList: [{ prop: 'campusId', label: '校区主键' }, { prop: 'counselor', label: '督导师' }, { prop: 'startDate', label: '督导开始日期' }, { prop: 'endDate', label: '督导截止日期' }, { prop: 'type', label: '督导类型' }, { prop: 'cooperate', label: '校区配合度' }, { prop: 'team', label: '校区师资团队' }, { prop: 'profit', label: '招生盈利' }, { prop: 'score', label: '督导评分' }],
      campusIdOptions: [],
      /*typeOptions: [
        { fullName: "教务督导", id: "1" },
        { fullName: "常规督导", id: "2" },
      ],*/
      campusOptions: [],
      brandOptions: [],
      campusLoading: false,
      typeOptions: [
        {
          fullName: '教务辅导',
          enCode: 1
        },
        {
          fullName: '日常督导',
          enCode: 2
        }
      ]
    }
  },
  computed: {},
  watch: {
    // async 'query.brand'(val) {
    //   // 统一品牌变更时重置校区并加载下拉
    //   this.query.campusId = undefined
    //   try {
    //     await this.handleChangeBrand(val)
    //   } catch (e) {
    //     // 已在 handleChangeBrand 做错误提示，这里避免重复提示
    //   }
    // }
  },
  mounted() {
    this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
      this.brandOptions = res
    })
  },
  created() {
    this.initData()
  },
  methods: {
    async loadCampusOptions() {
      try {
        this.campusLoading = true
        const res = await getCampusSelector({
          brand: this.query.brand,
          fields: 'id,fullName'
        })
        this.campusOptions = Array.isArray(res.data) ? res.data : []
      } catch (error) {
        console.error('加载校区数据失败:', error)
        this.$message.error('校区选项加载失败')
        this.campusOptions = []
      } finally {
        this.campusLoading = false
      }
    },
    initData() {
      this.listLoading = true
      let _query = {
        ...this.listQuery,
        ...this.query
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      getAcademic(query).then(res => {
        this.list = res.data.list
        this.total = res.data.pagination.total
        this.listLoading = false
      })
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        type: 'warning'
      })
        .then(() => {
          request({
            url: `/api/campus/supervise/${id}`,
            method: 'DELETE'
          }).then(res => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.initData()
              }
            })
          })
        })
        .catch(() => {})
    },
    handleSelectionChange(val) {
      const res = val.map(item => item.id)
      this.multipleSelection = res
    },
    handleBatchRemoveDel() {
      if (!this.multipleSelection.length) {
        this.$message({
          type: 'error',
          message: '请选择一条数据',
          duration: 1500
        })
        return
      }
      const ids = this.multipleSelection
      this.$confirm('您确定要删除这些数据吗, 是否继续？', '提示', {
        type: 'warning'
      })
        .then(() => {
          request({
            url: `/api/campus/supervise/batchRemove`,
            method: 'POST',
            data: ids
          }).then(res => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.initData()
              }
            })
          })
        })
        .catch(() => {})
    },
    addOrUpdateHandle(id, flowState) {
      let data = {
        id: id || '',
        enCode: '#visualDevAcademic',
        flowId: '522284735775376645',
        formType: 1,
        opType: flowState ? 0 : '-1',
        status: flowState
      }
      this.flowVisible = true
      this.$nextTick(() => {
        this.$refs.FlowBox.init(data)
      })
    },
    exportData() {
      this.exportBoxVisible = true
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList)
      })
    },
    download(data) {
      let query = { ...data, ...this.listQuery, ...this.query }
      request({
        url: `/api/campus/supervise/Actions/Export`,
        method: 'GET',
        data: query
      }).then(res => {
        if (!res.data.url) return
        window.location.href = this.define.comUrl + res.data.url
        this.$refs.ExportBox.visible = false
        this.exportBoxVisible = false
      })
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'createTime'
      }
      this.initData()
    },
    refresh(isrRefresh) {
      this.formVisible = false
      if (isrRefresh) this.reset()
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'createTime'
      }
      this.initData()
    },
    colseFlow(isrRefresh) {
      this.flowVisible = false
      if (isrRefresh) this.reset()
    }
  }
}
